// pages/platemetal/query_bind/query_bind.js

const behavior = getApp().require("behaviors/requests.js")
Page({
  /**
   * 页面的初始数据
   */
  behaviors: [behavior],
  data: {
    rfid1: "",
    rfid2: "",
    mac: "",
    query_mac:"",
    rfid: "",
    flag: false
  },

  scanMac() {
    const that = this
    wx.scanCode({
      success(res) {
        const data = res.result;
        console.log(res.result);
        that.setData({
          mac: data
        });
      }
    })
  },

  scanRfid() {
    const that = this
    wx.scanCode({
      success(res) {
        const data = res.result;
        console.log(res.result);
        that.setData({
          rfid: data
        });
      }
    })
  },

  search_by_rfid() {
    const that = this
    const url = 'https://logisticsengineering.gree.com/order_system/query_mac_from_rfid';
    const data = { rfid: that.data.rfid };
    this.post(url, data).then((res) => {
      wx.showToast({
        title: res.msg,
      });
      const data = res.data
      console.log(res)
      that.setData({
        rfid1: data.rfid1,
        rfid2: data.rfid2,
        query_mac: data.mac,
        flag: true
      })
    }).catch((err) => {
      console.log(err)
      wx.showToast({
        title: err.msg,
      });
    })
  },

  search_bind() {
    const that = this
    const url = 'https://logisticsengineering.gree.com/order_system/query_mac_bind_rfid';
    const data = { mac: that.data.mac };
    this.post(url, data).then((res) => {
      wx.showToast({
        title: res.msg,
      });
      const data = res.data
      console.log(data)
      that.setData({
        rfid1: data.rfid1,
        rfid2: data.rfid2,
        query_mac:that.data.mac,
        flag: true
      })
    }).catch((err) => {
      console.log(err)
      wx.showToast({
        title: err.msg,
      });
    })
  },

  delete() {
    const that = this
    wx.showModal({
      title: "删除提示",
      content: "确定删除？",
      success(modal) {

        if (modal.confirm) {
          const url = 'https://logisticsengineering.gree.com/order_system/delete_mac_bind_rfid';
          const data = { mac: that.data.query_mac };
          that.post(url, data).then((res) => {
            console.log(res);
            wx.showToast({
              title: res.msg,
            });
            if (res.code == 20000) {
              that.setData({
                rfid1: "",
                rfid2: "",
                query_mac:"",
                flag: false
              })
            }
          }).catch((err) => {
            console.log(err)
          })
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})